package com.waitingresult.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
/*
     // 单例模式
    public static final DateUtil instance = new DateUtil();
    private DateUtil(){

    }
    public static DateUtil getInstance(){
        return instance;
    }*/
    /**
     * 获取字符串 年月日
     *
     * @param day 加上几天 0就是当前时间
     * @return
     */
    public static String getYMDAddValue(Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, day);
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        return fmt.format(calendar.getTime());
    }

    /**
     * 获取字符串 月日
     */
    public static String getMonthDay(Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, day);
        SimpleDateFormat fmt = new SimpleDateFormat("MM-dd");
        return fmt.format(calendar.getTime());
    }

    /**
     * 通过一个字符串 转换得到时间戳
     *
     * @param ymdHs yyyy-MM-dd HH:mm 字符串格式时间
     * @return
     */
    public static Integer stringDate2Timestamp(String ymdHs) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = simpleDateFormat.parse(ymdHs);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Math.toIntExact(date.getTime() / 1000);
    }

    /**
     * 获取当前月的剩余天数
     */
    public static Integer getThisMonthSurplusDayNum() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int thisDayNum = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        int month = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return month - thisDayNum;
    }

    /**
     * 传入时间返回星期几
     */
    public static String getWhatDayStr(String ymd) {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(ymd);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        return c.get(Calendar.DAY_OF_WEEK) + "";
    }
}
